package com.shujia.day11;

import java.util.ArrayList;
import java.util.HashMap;

public class Demo02Java2SQL {
    public static void main(String[] args) {
        /*
            TODO 现阶段大家能完成的需求
                数据总量是一定的  可以保存到集合中
                对批量数据进行做判断
                    排序 最大值 最小值 关联 分组求和 数据过滤 排名
                    SQL =>
                        (FROM 数据加载？ 可以从控制台输入数据)
                        JOIN  ON 关联 => 两个集合之间的遍历
                        WHERE 过滤 => 集合删除
                        GROUP BY 分组 => 使用Map+List结构
                        HAVING 统计过滤 => 删除
                        ORDER BY 排序 => sort函数 TreeMap ..
                        Limit 限制数据 => for循环遍历
                   思考上述过程如何实现
         */

        /*
          TODO  关联 分组求和
                 1.先创建Score的数组，之后再关联学生基本信息数据

              注意：列编辑再vsCode中可以使用 shift + alt
         */
        ArrayList<Score> list = new ArrayList<>();
        list.add(new Score("1500100001", "1000001", 98));
        list.add(new Score("1500100001", "1000002", 5));
        list.add(new Score("1500100001", "1000003", 137));
        list.add(new Score("1500100001", "1000004", 29));
        list.add(new Score("1500100001", "1000005", 85));
        list.add(new Score("1500100001", "1000006", 52));
        list.add(new Score("1500100002", "1000001", 139));
        list.add(new Score("1500100002", "1000002", 102));
        list.add(new Score("1500100002", "1000003", 44));
        list.add(new Score("1500100002", "1000004", 18));
        list.add(new Score("1500100002", "1000005", 46));
        list.add(new Score("1500100002", "1000006", 91));
        list.add(new Score("1500100003", "1000001", 48));
        list.add(new Score("1500100003", "1000002", 132));
        list.add(new Score("1500100003", "1000003", 41));
        list.add(new Score("1500100003", "1000007", 32));
        list.add(new Score("1500100003", "1000008", 7));
        list.add(new Score("1500100003", "1000009", 99));
        list.add(new Score("1500100004", "1000001", 147));
        list.add(new Score("1500100004", "1000002", 69));
        list.add(new Score("1500100004", "1000003", 37));
        list.add(new Score("1500100004", "1000007", 87));
        list.add(new Score("1500100004", "1000008", 21));
        list.add(new Score("1500100004", "1000009", 60));
        list.add(new Score("1500100005", "1000001", 105));
        list.add(new Score("1500100005", "1000002", 105));
        list.add(new Score("1500100005", "1000003", 30));
        list.add(new Score("1500100005", "1000007", 68));
        list.add(new Score("1500100005", "1000008", 38));
        list.add(new Score("1500100005", "1000009", 49));
        list.add(new Score("1500100006", "1000001", 87));
        list.add(new Score("1500100006", "1000002", 98));
        list.add(new Score("1500100006", "1000003", 55));
        list.add(new Score("1500100006", "1000007", 44));
        list.add(new Score("1500100006", "1000008", 1));
        list.add(new Score("1500100006", "1000009", 29));
        list.add(new Score("1500100007", "1000001", 102));
        list.add(new Score("1500100007", "1000002", 149));
        list.add(new Score("1500100007", "1000003", 83));
        list.add(new Score("1500100007", "1000004", 21));
        list.add(new Score("1500100007", "1000005", 25));
        list.add(new Score("1500100007", "1000006", 38));
        list.add(new Score("1500100008", "1000001", 5));
        list.add(new Score("1500100008", "1000002", 129));
        list.add(new Score("1500100008", "1000003", 110));
        list.add(new Score("1500100008", "1000007", 47));
        list.add(new Score("1500100008", "1000008", 40));
        list.add(new Score("1500100008", "1000009", 32));
        list.add(new Score("1500100009", "1000001", 65));
        list.add(new Score("1500100009", "1000002", 71));
        list.add(new Score("1500100009", "1000003", 38));
        list.add(new Score("1500100009", "1000007", 57));
        list.add(new Score("1500100009", "1000008", 17));
        list.add(new Score("1500100009", "1000009", 3));


        Student student1 = new Student("1500100001", "施笑槐", 22, "女", "文科六班");
        Student student2 = new Student("1500100002", "吕金鹏", 24, "男", "文科六班");
        Student student3 = new Student("1500100003", "单乐蕊", 22, "女", "理科六班");
        Student student4 = new Student("1500100004", "葛德曜", 24, "男", "理科六班");
        Student student5 = new Student("1500100005", "宣谷芹", 21, "女", "理科六班");
        Student student6 = new Student("1500100006", "边昂雄", 22, "男", "理科六班");
        Student student7 = new Student("1500100007", "尚孤风", 23, "女", "文科六班");
        Student student8 = new Student("1500100008", "符半双", 22, "女", "文科六班");
        Student student9 = new Student("1500100009", "沈德昌", 21, "男", "文科六班");

        ArrayList<Student> stuList = new ArrayList<>();
        stuList.add(student1);
        stuList.add(student2);
        stuList.add(student3);
        stuList.add(student4);
        stuList.add(student5);
        stuList.add(student6);
        stuList.add(student7);
        stuList.add(student8);
        stuList.add(student9);

        // TODO 关联:
        //     对于如下方式在获取数据时，需要将所有的数据进行遍历，遍历完成后再取出
//        for (Score score : list) {
//            String id = score.id;
//            for (Student student : stuList) {
//                if (student.id.equals(id)) {
//                    System.out.println("" + score + student);
//                }
//            }
//        }

        // 将stuList改成HashMap
        // 以ID作为Key 学生基本信息数据作为Value
        HashMap<String, Student> hashMap = new HashMap<>();
        for (Student student : stuList) {
            hashMap.put(student.id, student);
        }
        System.out.println("=======================================");
        // 所有的成绩数据进行遍历 再去从学生基本信息的HashMap中进行获取对应的KeyValue数据
        for (Score score : list) {
            Student student = hashMap.get(score.id);
            System.out.println("" + score + student);
        }

        /*
            TODO 分组求和
                分析：
                    1.将所有相同学生的ID数据汇集到一起，再将所有的成绩数据进行累加
                    2.将得到的累加和填入到Student对象中
         */
        HashMap<String, Double> doubleHashMap = new HashMap<>();
        for (Score score : list) {
            if (!doubleHashMap.containsKey(score.id)) {
                doubleHashMap.put(score.id,score.score);
            }else {
                Double sumScore = doubleHashMap.get(score.id);
                doubleHashMap.put(score.id,sumScore + score.score);
            }
        }

        for (Student student : stuList) {
            student.totalScore = doubleHashMap.get(student.id);
        }

        for (Student student : stuList) {
            System.out.println("学生数据:"+student);
        }







    }
}

class Score {
    // 1500100009,1000002,71
    String id;
    String courseId;
    double score;

    public Score(String id, String courseId, double score) {
        this.id = id;
        this.courseId = courseId;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Score{" +
                "id='" + id + '\'' +
                ", courseId='" + courseId + '\'' +
                ", score=" + score +
                '}';
    }



}
